package iSOCK::Controller::Logs;

use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller' }

use HTML::Entities qw( encode_entities );
use iSOCK::Controller::Root;
use Regexp::Common qw( URI );

=head1 NAME

iSOCK::Controller::Logs - Logs Controller for iSOCK

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

sub index :Path {
  my ( $self, $c ) = @_;
  
  my $session_id   = $c->sessionid;
  my $session_path = iSOCK::Controller::Root->session_path ( $c );
  
  my @log_stems = @{$c->config->{log_stems}};

  my $span = sub {
    my $map = { W => 'warning', E => 'error', F => 'fatal' };

    return '' if !defined $_[0] || $_[0] eq '';

    my $line;

    for ( encode_entities ( $_[0] ) ) {
      s/     '  ( $RE{URI}{HTTP} )    '  /'<a href='$1'>$1<\/a>'/gx;
      s/ (?<!') ( $RE{URI}{HTTP} ) (?!') /<a href='$1'>$1<\/a>/gx;

      m/^-([A-Z])-/;
      no warnings 'uninitialized';
      my $class = $map->{$1} || ( m/ [:|] \s+ KO \b /x ? 'error' : '' );

      $line = "<span" . ( $class ? " class='$class'" : '' ) . ">$_</span>\n";
    }

    return $line;
  };

  my ( $body, @components );

  for my $log_file ( grep { -f "$session_path/$_" } map { "$_.log" } @log_stems ) {
    $body .= "      <h2 id='${log_file}_marker'>$log_file</h2>\n\n";

    if ( open my $fh, '<', "$session_path/$log_file" ) {
      $body .= "      <div class='boxed'>\n";
      $body .= "        <div class='inside'>\n";
      $body .= "          <pre>";

      while ( <$fh> ) {
        chomp;

        my $show = 1;

        if ( $log_file eq 'sock_followup_to_whatever.log' ) {
          if ( my ( $component ) = ( m/^-I- \| Processing (\w+) component\.\.\./ ) ) {
            push @components, { id => $component };

            $body .= "<a id='$component'><\/a>";
          }
          elsif ( m/^-I- \| Parallel processing started\.\.\./ .. m/^-I- \| Done with parallel processing\./ ) {
            $show = 0 unless m/\b [Pp]arallel \b/x;
          }

          if ( m/^-I- \| Done with parallel processing\./ ) {
            for my $component_log_file ( glob "$session_path/3_LOGs_out/sock_followup_to_whatever-*.log" ) {
              my ( $component ) = ( $component_log_file =~ /- (\w+) \.log $/x );

              push @components, { id => $component };

              $body .= "<a id='$component'><\/a>";

              if ( open my $fh_component, '<',  $component_log_file ) {
                while ( <$fh_component> ) {
                  chomp;

                  $body .= $span->( $_ );
                }
              }
              else {
                $body .= $span->( "-E- | | Could not open $component LOG file!" );
              }
            }
          }
        }
        elsif ( $log_file eq 'sock_annotate_datasheets.log' ) {
          if ( m/^-I- \| \| Parallel processing started\.\.\./ .. m/^-I- \| \| Done with parallel processing\./ ) {
            $show = 0 unless m/\b [Pp]arallel \b/x;
          }

          if ( m/^-I- \| \| Done with parallel processing\./ ) {
            for my $component_log_file ( glob "$session_path/5_LOGs_out/sock_annotate_datasheets-*.log" ) {
              my ( $component ) = ( $component_log_file =~ /- (\w+) \.log $/x );

              if ( open my $fh_component, '<',  $component_log_file ) {
                while ( <$fh_component> ) {
                  chomp;

                  $body .= $span->( $_ );
                }
              }
              else {
                $body .= $span->( "-E- | | | Could not open $component LOG file!" );
              }
            }
          }
        }

        if ( $show ) {
          $body .= $span->( $_ );

          if ( $log_file =~ /^isock_/ ) {
            $body =~ s/ ( Spawning \s (?:'|"|&quot;|&\#39;) ) ( i? sock_\w+ ) /$1<a href='#$2.log_marker'>$2<\/a>/xg;
          }
        }
      }

      chomp $body;

      $body .= "</pre>\n";
      $body .= "        </div>\n";
      $body .= "        <div class='$_'></div>\n" for qw( top_right top_left bottom_right bottom_left );
      $body .= "      </div>\n";
    }
    else {
      $body .= "      <p class='error'>Could not open '$session_path/$log_file' file!</p>";
    }
  }
  
  $c->stash ( body => $body, alt_navigation => ( scalar @components ? 1 : undef ), alt_navigation_title => 'Components', alt_navigation_items => \@components );
  
  $c->forward ( qw( iSOCK::Controller::Root render_view ) );
}

sub end : ActionClass('RenderView') {}

=head1 AUTHOR

Xavier Caron

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
